package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.ProgramList;

import db.DbConn;

public class NewStart extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	DbConn conn;

	@Override
	public void init() throws ServletException {
		conn = new DbConn();
		conn.setDbUrl(getServletContext().getInitParameter("dbUrl"));
		conn.setDbUser(getServletContext().getInitParameter("dbUser"));
		conn.setDbPass(getServletContext().getInitParameter("dbPass"));
	}

	// Index 에서 servlet 으로 href 링크를 타고 넘어온 경우
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		HttpSession session = req.getSession();
		String userId = (String) session.getAttribute("userId");
		Boolean isDoingProject = conn.getProgramIdFromProject(userId) != null ?true : false;

		// 현재 사용자가 진행중인 프로젝트가 있다 -> error -> page redirect to index
		if (isDoingProject == true) {
			resp.setContentType("text/html");
			resp.setCharacterEncoding("UTF-8");
			PrintWriter out = resp.getWriter();
			out.println("<script type='text/javascript'>");
			out.println("alert('진행중인 프로젝트가 있습니다! 새로운 프로젝트를 진행하려면 이전 프로젝트를 포기하십시오');");
			out.println("location.href='logonIndex.jsp';");
			out.println("</script>");
			out.flush();
		}
		// 현재 사용자가 진행중인 프로젝트가 없다.
		else {
			ArrayList<ProgramList> arr = new ArrayList<ProgramList>();
			arr = conn.getProgramList();
			req.setAttribute("programList", arr);
			RequestDispatcher dispatcher = req
					.getRequestDispatcher("newstart.jsp");
			dispatcher.forward(req, resp);
		}
	}

	// newstart.jsp 에서 form action 을 통해서 넘어온 경우
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// 1. form data를 불러온다.
		// 2. projectuserlist table 에 사용자를 저장한다.
		try {
			HttpSession session = req.getSession();
			String userId = (String) session.getAttribute("userId");
			int now = Integer.parseInt(req.getParameter("nowWeight"));
			int goal = Integer.parseInt(req.getParameter("goalWeight"));
			String programId = req.getParameter("programId");
			int totalDate = conn.getTotalDateFromProgramList(programId);
			if (totalDate != -1) {
				System.out.println("userId :"+ userId + " now : " + now + " goal: " + goal
						+ " programId " + programId + " totaldate :" + totalDate);
				conn.setProjectUserList(programId, userId, now, goal, totalDate);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 3. forward 시킨다.
		RequestDispatcher dispatcher = req
				.getRequestDispatcher("logonIndex.jsp");
		dispatcher.forward(req, resp);
	}
}
